package com.udaos.modules.call.controllers;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.call.Ud_call;
import com.udaos.models.call.Ud_call_record;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.call.services.UdCallRecordService;
import com.udaos.modules.call.services.UdCallService;

/**
 * 来电记录
 * @author mutaotao
 *
 */
@IocBean
@At("/platform/call")
@Filters({ @By(type = PrivateFilter.class)})
public class UdCallController {
	
	@Inject
	private UdCallService callService;
	@Inject
	private UdCallRecordService callRecordService;
	
	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/call/index.html")
	@RequiresAuthentication
	public void index() {
		
	}
	
	/**
	 * 加载数据
	 * @param name
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("name") String name,@Param("mobile") String mobile,@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		cnd.and("delFlag", "=", "0");
		cnd.desc("createAt");
		if (!Strings.isBlank(name))
			cnd.and("name", "like", "%" + name + "%");
		if (!Strings.isBlank(mobile))
			cnd.and("mobile", "like", "%" + mobile + "%");
		return callService.data(length, start, draw, order, columns, cnd, null);
	}
	
	/**
	 * 新增
	 * @param unitid
	 * @return
	 */
	@At
	@Ok("beetl:/platform/call/add.html")
	@RequiresAuthentication
	public void add() {
		
	}
	
	/**
	 * 新增
	 * @param company
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object addDo(@Param("..") Ud_call call_record, @Param("createAtStr") String createAtStr,HttpServletRequest req) {
		try {
			//设置创建人
			Subject subject = SecurityUtils.getSubject();
            Sys_user user = (Sys_user) subject.getPrincipal();
            call_record.setCreateBy(user.getId());
            //设置创建时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			int createAt = (int) (sdf.parse(createAtStr).getTime() / 1000);
			call_record.setCreateAt(createAt);
			callService.insert(call_record);
	        return Result.success("保存成功");
	    } catch (Exception e) {
	    	return Result.error(e.getMessage());
	    }
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@At("/edit/?")
	@Ok("beetl:/platform/call/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("call", callService.fetch(id));
		//获取记录
		List<Ud_call_record> records=callRecordService.findById(id);
		map.put("records", records);
		return map;
	}
	
	/**
	 * 新增
	 * @param company
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object editDo(@Param("..") Ud_call call_record, @Param("createAtStr") String createAtStr,HttpServletRequest req) {
		try {
			//设置创建人
			Subject subject = SecurityUtils.getSubject();
            Sys_user user = (Sys_user) subject.getPrincipal();
            call_record.setCreateBy(user.getId());
            //设置创建时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			int createAt = (int) (sdf.parse(createAtStr).getTime() / 1000);
			call_record.setCreateAt(createAt);
			callService.updateIgnoreNull(call_record);
	        return Result.success("保存成功");
	    } catch (Exception e) {
	    	return Result.error(e.getMessage());
	    }
	}
	
	/**
	 * 删除
	 * @param id
	 * @param req
	 * @return
	 */
	/*@At("/delete/?")
    @Ok("json")
	@RequiresAuthentication
    public Object delete(String id, HttpServletRequest req) {
        try {
        	Ud_call call_record = callService.fetch(id);
        	call_record.setDelFlag(true);
        	callService.updateIgnoreNull(call_record);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
	*/
	
	/**
	 * 新增访问记录
	 * @param unitid
	 * @return
	 */
	@At("/recordAdd/?")
	@Ok("beetl:/platform/call/recordadd.html")
	@RequiresAuthentication
	public Object recordAdd(String id) {
		return callService.fetch(id);
	}
	
	/**
	 * 新增
	 * @param call_record
	 * @param createAtStr
	 * @param req
	 * @return
	 */
	 
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object recordAddDo(@Param("..") Ud_call_record call_record, @Param("createAtStr") String createAtStr,HttpServletRequest req) {
		try {
            //设置创建时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			int createAt = (int) (sdf.parse(createAtStr).getTime() / 1000);
			call_record.setCreateAt(createAt);
			callRecordService.insert(call_record);
	        return Result.success("保存成功");
	    } catch (Exception e) {
	    	return Result.error(e.getMessage());
	    }
	}

}
